package cn.hr.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import cn.hr.dao.mapper.MissionTypeMapper;
import cn.hr.dao.mapper.SalProStanMapper;
import cn.hr.pojo.MissionType;
import cn.hr.pojo.PayrollCrew;
import cn.hr.pojo.SalProStan;
import cn.hr.utils.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import cn.hr.dao.mapper.SalaryStandardMapper;
import cn.hr.pojo.SalaryStandard;
import cn.hr.service.SalaryStandardService;

/**
 * 薪酬标准服务接口实现类
 *
 * @author MO
 */
@CacheConfig(cacheNames = "salaryStandard")
@Scope("prototype")
@Service("salaryStandardService")
public class SalaryStandardServiceImpl implements SalaryStandardService {
    @Resource
    private SalaryStandardMapper salaryStandardMapper;
    @Resource
    private SalProStanMapper salProStanMapper;
    @Resource
    private MissionTypeMapper missionTypeMapper;

    @Override
    public boolean insert(SalaryStandard t) throws Exception {
        if (salaryStandardMapper.findMaxId() == null) {
            t.setCode(Constants.SALARYSTANDARD_CODE + (1));
        } else {
            t.setCode(Constants.SALARYSTANDARD_CODE + (salaryStandardMapper.findMaxId() + 1));
        }
        t.setCreatedDate(new Date());
        int num = salaryStandardMapper.insert(t);
        if (num <= 0) {
            return false;
        } else {
            for (SalProStan salProStan : t.getSalProStenList()) {
                salProStan.setSalS(t.getId());
                if (salProStanMapper.insert(salProStan) <= 0) {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    @CacheEvict(value = {"salaryStandard", "salProStan", "mission"}, allEntries = true)
    public boolean update(SalaryStandard t) throws Exception {
        t.setModifyDate(new Date());
        if (salaryStandardMapper.update(t) != 1) {
            return false;
        } else {
            for (SalProStan salProStan : t.getSalProStenList()) {
                if (salProStanMapper.update(salProStan) <= 0) {
                    return false;
                }
            }
        }
        if (t.getAuditStatus() == 12) {
            MissionType missionType = new MissionType();
            missionType.setCreatedById(t.getModifyById());
            missionType.setCreatedDate(new Date());
            missionType.setName(t.getName() + "薪酬标准审核");
            missionType.setTypeId(38);
            missionType.setTableId(t.getId());
            if (missionTypeMapper.insert(missionType) != 1) {
                return false;
            }
        } else if (t.getAuditStatus() == 11) {
            if (missionTypeMapper.deleteByTableIdAndTypeId(38, t.getId()) <= 0) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean delete(Integer id) throws Exception {
        return salaryStandardMapper.delete(id) == 1;
    }

    @Override
    public SalaryStandard get(Integer id) throws Exception {
        return salaryStandardMapper.get(id);
    }

    @Override
    public PageInfo<Map> findListByCondition(Map m, int pageNo) throws Exception {
        PageHelper.startPage(pageNo, Constants.PAGESIZE);
        List<Map> list = salaryStandardMapper.findListByCondition(m);
        return new PageInfo<Map>(list);
    }

    @Override
    public Long findCountByCondition(Map m) throws Exception {
        return salaryStandardMapper.findCountByCondition(m);
    }

    @Override
    public List<SalaryStandard> selectAllIdAndName() {
        return salaryStandardMapper.selectAllIdAndName();
    }
}
